package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Customer;
import cn.wolfcode.rbac.domain.Customertransfer;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.ICustomerService;
import cn.wolfcode.rbac.service.ICustomertransferService;
import cn.wolfcode.rbac.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customerTransfer")
public class CustomertransferController {
    @Autowired
    private ICustomertransferService customertransferService;
    @Autowired
    private ICustomerService customerService;
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model){
        System.out.println(qo.getKeyword());
        PageInfo pageInfo = customertransferService.selectAll(qo);
        System.out.println("--------------"+pageInfo.toString());
        model.addAttribute("result",pageInfo);
        return "customerTransfer/list";
    }

    //移交
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(@Param("customertransfer") Customertransfer customertransfer,Long oldSeller_id){
        try{
            System.out.println(oldSeller_id);
            customertransfer.getOldSeller().setId(oldSeller_id);
            this.customertransferService.turnOver(customertransfer);
            return new JsonResult("操作成功", true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("操作失败", false);
        }
    }

    //吸纳
    @RequestMapping("/absorb")
    @ResponseBody
    public JsonResult absorb(Customertransfer customertransfer){
        try{
            customertransfer.setNewSeller(customertransfer.getOldSeller());
            this.customerService.absorb(customertransfer);
            this.customertransferService.turnOver(customertransfer);
            return new JsonResult("操作成功", true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("操作失败", false);
        }
    }
}
